package com.yc.cloud.openai.portal.controller;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yc.cloud.common.basic.api.ResponseResult;
import com.yc.cloud.common.basic.dto.BaseQueryParam;
import com.yc.cloud.common.orm.CommonPage;
import com.yc.cloud.openai.portal.dto.gpt.GptDatasetUpdateParam;
import com.yc.cloud.openai.portal.dto.request.GptDatasetCreateParam;
import com.yc.cloud.openai.portal.dto.request.GptDatasetDeleteParam;
import com.yc.cloud.openai.portal.enums.GPTChatProvider;
import com.yc.cloud.openai.portal.service.GptDatasetService;
import com.yc.cloud.openai.portal.vo.gpt.GptDatasetVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import lombok.val;

/**
 * 问答知识库
 * 
 * @author Yancy
 * @date 2025-01-07
 */
@RestController
@RequestMapping("chat/dataset")
@Api(value = "问答知识库", tags = "问答知识库")
public class GptDatasetController {

        @Resource
        private GptDatasetService gptDatasetService;

        @GetMapping
        @ApiOperation(value = "问答知识库列表", notes = "支持分页查询,字段模糊匹配,排序等")
        public ResponseResult<CommonPage<GptDatasetVo>> list(@Validated BaseQueryParam param) {
                val result = gptDatasetService.list(param);
                return ResponseResult.success(CommonPage.restPage(result));
        }

        @GetMapping("{id}")
        @ApiOperation(value = "问答知识库详情", notes = "根据ID查询")
        public ResponseResult<GptDatasetVo> get(@PathVariable @ApiParam(value = "ID", required = true) Long id) {
                val result = gptDatasetService.get(id);
                return ResponseResult.success(result);
        }

        @PostMapping
        @ApiOperation(value = "添加问答知识库", notes = "添加")
        public ResponseResult<GptDatasetVo> create(@Validated @RequestBody GptDatasetCreateParam param) {
                val result = gptDatasetService.create(param);
                return result != null ? ResponseResult.success(result) : ResponseResult.failed();
        }

        @ApiOperation(value = "删除问答知识库", notes = "通过ID删除")
        @DeleteMapping(value = "{id}")
        public ResponseResult<Boolean> delete(@PathVariable @ApiParam(value = "id", required = true) Long id) {
                val param = new GptDatasetDeleteParam();
                param.setProvider(GPTChatProvider.FASTGPT.getKey());
                val result = gptDatasetService.delete(id, param);
                return result ? ResponseResult.success(true) : ResponseResult.failed();
        }

        @ApiOperation(value = "修改问答知识库", notes = "通过ID修改")
        @PutMapping(value = "{id}")
        public ResponseResult<GptDatasetVo> update(@PathVariable @ApiParam(value = "ID", required = true) Long id,
                        @Validated @RequestBody GptDatasetUpdateParam param) {
                val result = gptDatasetService.update(id, param);
                return result != null ? ResponseResult.success(result) : ResponseResult.failed();
        }
}
